import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 文件上传 demo
 */
@MultipartConfig
@WebServlet("/upload")
@SuppressWarnings({"all"})
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part myImage = req.getPart("MyImage");
        System.out.println(myImage.getSubmittedFileName());
        System.out.println(myImage.getContentType());
        System.out.println(myImage.getSize());
        String url = "D:/javacode/javaee/MyServlet/src/main/webapp/image/" + myImage.getSubmittedFileName(); // 设置写入路径
        myImage.write(url);
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("上传成功!");
    }
}
